package com.ruoyi.gd.lswgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.gd.wgd.domain.ZWgd;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gd.lswgd.domain.bo.ZLswgdBo;
import com.ruoyi.gd.lswgd.domain.vo.ZLswgdVo;
import com.ruoyi.gd.lswgd.domain.ZLswgd;
import com.ruoyi.gd.lswgd.mapper.ZLswgdMapper;
import com.ruoyi.gd.lswgd.service.IZLswgdService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 历史完工单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-30
 */
@RequiredArgsConstructor
@Service
public class ZLswgdServiceImpl implements IZLswgdService {

    private final ZLswgdMapper baseMapper;

    /**
     * 查询历史完工单
     */
    @Override
    public ZLswgdVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询历史完工单列表
     */
    @Override
    public TableDataInfo<ZLswgdVo> queryPageList(ZLswgdBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZLswgd> lqw = buildQueryWrapper(bo);
        Page<ZLswgdVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询历史完工单列表
     */
    @Override
    public List<ZLswgdVo> queryList(ZLswgdBo bo) {
        LambdaQueryWrapper<ZLswgd> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZLswgd> buildQueryWrapper(ZLswgdBo bo) {
        Map<String, Object> params = bo.getParams();
        LoginUser loginUser = LoginHelper.getLoginUser();
        LambdaQueryWrapper<ZLswgd> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBjlx()), ZLswgd::getBjlx, bo.getBjlx());
        lqw.eq(StringUtils.isNotBlank(bo.getBz()), ZLswgd::getBz, bo.getBz());
        lqw.eq(StringUtils.isNotBlank(bo.getKhid()), ZLswgd::getKhid, bo.getKhid());
        lqw.eq(StringUtils.isNotBlank(bo.getKhbh()), ZLswgd::getKhbh, bo.getKhbh());
        lqw.like(StringUtils.isNotBlank(bo.getKhname()), ZLswgd::getKhname, bo.getKhname());
        lqw.eq(StringUtils.isNotBlank(bo.getFjid()), ZLswgd::getFjid, bo.getFjid());
        lqw.eq(bo.getLswgdid() != null, ZLswgd::getLswgdid, bo.getLswgdid());
        lqw.eq(StringUtils.isNotBlank(bo.getZbjdid()), ZLswgd::getZbjdid, bo.getZbjdid());
        if(!"admin".equals(loginUser.getUsername())) {
            List<String> strings = Arrays.asList("0");
            lqw.in(ZLswgd::getDelFlag, strings);
        }
        return lqw;
    }

    /**
     * 新增历史完工单
     */
    @Override
    public Boolean insertByBo(ZLswgdBo bo) {
        ZLswgd add = BeanUtil.toBean(bo, ZLswgd.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改历史完工单
     */
    @Override
    public Boolean updateByBo(ZLswgdBo bo) {
        ZLswgd update = BeanUtil.toBean(bo, ZLswgd.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZLswgd entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除历史完工单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.batchDeleteIds(ids) > 0;
    }
}
